package yz.zookeeper;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

public class zookeeperDemo1 {
    public static void main(String[] args) throws IOException, InterruptedException {
        //创建连接
        //public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher)
        //String connectString 表示zookeeper的连接信息
        //int sessionTimeout  表示的是会话的超时时间
        //Watcher watcher  监控器的对象
        ZooKeeper zooKeeper = new ZooKeeper("master:2181,node1:2181,node2:2181", 15000, new Watcher() {
            //对于接口对象创建时，要实现抽象函数
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("连接在变化");
            }
        });
        System.out.println("代码正在执行");

        zooKeeper.close();
        //异步性
        //代码正在执行
        //2023-08-11 21:12:51,475 DEBUG org.apache.zookeeper.ZooKeeper: Closing session: 0x0
        //2023-08-11 21:12:51,475 DEBUG org.apache.zookeeper.ClientCnxn: Closing client for session: 0x0
        //2023-08-11 21:12:51,482 DEBUG org.apache.zookeeper.SaslServerPrincipal: Canonicalized address to node2
        //2023-08-11 21:12:51,484 INFO org.apache.zookeeper.ClientCnxn: Opening socket connection to server node2/192.168.147.102:2181.
        //2023-08-11 21:12:51,484 INFO org.apache.zookeeper.ClientCnxn: SASL config status: Will not attempt to authenticate using SASL (unknown error)
        //2023-08-11 21:12:51,487 INFO org.apache.zookeeper.ClientCnxn: Socket connection established, initiating session, client: /192.168.147.1:63924, server: node2/192.168.147.102:2181
        //2023-08-11 21:12:51,488 DEBUG org.apache.zookeeper.ClientCnxn: Session establishment request sent on node2/192.168.147.102:2181
        //2023-08-11 21:12:51,507 INFO org.apache.zookeeper.ClientCnxn: Session establishment complete on server node2/192.168.147.102:2181, session id = 0x2000019949f0000, negotiated timeout = 15000
        //连接在变化
        //2023-08-11 21:12:51,520 DEBUG org.apache.zookeeper.ClientCnxn: Reading reply session id: 0x2000019949f0000, packet:: clientPath:null serverPath:null finished:false header:: 1,-11  replyHeader:: 1,8589934595,0  request:: null response:: null
        //2023-08-11 21:12:51,520 WARN org.apache.zookeeper.ClientCnxn: An exception was thrown while closing send thread for session 0x2000019949f0000.
        //EndOfStreamException: Unable to read additional data from server sessionid 0x2000019949f0000, likely server has closed socket
        //	at org.apache.zookeeper.ClientCnxnSocketNIO.doIO(ClientCnxnSocketNIO.java:77)
        //	at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:350)
        //	at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1282)
        //2023-08-11 21:12:51,521 DEBUG org.apache.zookeeper.ClientCnxn: Disconnecting client for session: 0x2000019949f0000
        //连接在变化
        //2023-08-11 21:12:51,631 INFO org.apache.zookeeper.ZooKeeper: Session: 0x2000019949f0000 closed
    }
}
